<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>课程列表</title>
</head>
<body>
<%
    String url = "jdbc:mysql://localhost:3306/studentdb?serverTimezone=UTC";
    String db_name = "root";
    String db_pass = "123456";
    String sql = "select * from class";
%>
<form>
<label for="classname">课程名称：</label>
<input type="text" id="classname" name="classname" required>
    <button onclick="">搜索</button>
</form>
<br><br>
<table width="100%" cellspacing="" cellpadding="15" border="" style="color:#333333;border-collapse:collapse;">
    <tr style="color:White;background-color:#027DB4;font-weight:bold;">
        <th>全选</th>
        <th>课程编号</th>
        <th>课程名称</th>
        <th>任课教师</th>
        <th>课程学分</th>
        <th>操作</th>
    </tr>
    <tr style="color:#333333;background-color:#FFFBD6;">
        <td><input type="checkbox" /></td>
        <td>1</td>
        <td>java软件设计</td>
        <td>黄轩</td>
        <td>6</td>
        <td>
            <button onclick="deleteCourse(1)">删除</button>&nbsp;&nbsp;
            <button onclick="">提交</button>
        </td>
    </tr>
    <%
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection conn = DriverManager.getConnection(url, db_name, db_pass);
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(sql);
            while (rs.next()) {
    %>
    <tr style="color:#333333;background-color:#FFFBD6;">
        <td><input type="checkbox" /></td>
        <td><%= rs.getString(1) %></td>
        <td><%= rs.getString(2) %></td>
        <td><%= rs.getString(3) %></td>
        <td><%= rs.getString(4) %></td>
        <td>
            <button onclick="deleteCourse(<%= rs.getInt(1) %>)">删除</button>&nbsp;&nbsp;
            <button onclick="">提交</button>
        </td>
    </tr>
    <%
            }
            stmt.close();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    %>
</table>

<script>
    function deleteCourse(classid) {
        if (confirm("确定要删除该课程吗？")) {
            var xhr = new XMLHttpRequest();
            xhr.open("POST", "DeleteClassServlet", true);
            xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            xhr.onreadystatechange = function() {
                if (xhr.readyState == 4 && xhr.status == 200) {
                    location.reload();
                }
            };
            xhr.send("classid=" + classid);
        }
    }
</script>
</body>
</html>
